import axios from 'axios'
import QS from 'qs'
import { showSuccessToast, showFailToast, showToast, showLoadingToast, closeToast } from "vant";
import { ElMessage } from "element-plus";
import { i18n } from '@/i18n'
import { isMobile } from "@/utils/tools"

// 请求基准路径
// if (import.meta.env.VITE_BASE_URL){
//   axios.defaults.baseURL = import.meta.env.VITE_BASE_URL;
// }
const message = (text) => {
  if(isMobile()) {
    showToast(text)
  } else {
    ElMessage.warning(text)
  }
}

const langHanding = (text) => {
  if(i18n.global.locale.value === "en") {
    switch (text) {
      case '手机验证码不能为空！': {
        return i18n.global.t('verificationCodeEmpty')
      }
      case '发送验证码失败！': {
        return i18n.global.t('verificationCodeFailed')
      }
      case '验证码错误': {
        return i18n.global.t('verificationCodeIncorrect')
      }
      default: {
        return text
      }
    }
  } else {
    return text
  }
}

// 请求超时时间
axios.defaults.timeout = 300000;

// 请求拦截器
axios.interceptors.request.use(
  config => {
    if(isMobile()) {
      showLoadingToast({
        duration: 0,
        forbidClick: true,
      });
    }
    // if (getToken()) {
    //   config.headers["Authorization"] = getToken()
    // }
    config.headers["Cache-control"] = "max-age=3600"
    config.headers["Access-Control-Allow-Origin"] = "*"

    return config
  },
  error => {
    return Promise.error(error);
  }
)

// 响应拦截器
axios.interceptors.response.use(
  response => {
    // console.log(response, '......http.response')
    closeToast()
    const res = response.data
    if (
      res.result == 200 ||
      res.result === 200 ||
      res.result == 0 ||
      res.code === 0 ||
      res.code == 200
    ) {
      return res;
    } else if (res.result === "998") {
      return res;
    } else if (res.access_token) {
      return res;
    } else {
      if (!res.result && !res.code && response.status == 200) {
        // showFailToast({
        //   message: langHanding(res.data),
        //   wordBreak: 'break-word',
        //   className: "toastWidth",
        //   duration: 100000
        // })
        message(langHanding(res.data))
        return Promise.reject(res);
      }
    }
    // showFailToast({
    //   message: langHanding(res.data),
    //   wordBreak: 'break-word',
    //   className: "toastWidth",
    //   duration: 100000
    // })
    message(langHanding(res.data))
    return Promise.reject(res)
  },
  error => {
    // console.log(error, '......http.error')
    // showFailToast({
    //   message: langHanding(error.message),
    //   wordBreak: 'break-word',
    //   className: "toastWidth",
    //   duration: 100000
    // })
    message(langHanding(error.message))
    return Promise.reject(error)
  }
)

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url, params) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
        params: params
      })
      .then(res => {
        resolve(res);
      })
      .catch(err => {
        reject(err)
      })
  });
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */

export function jsonPost(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, params, {
        headers: {
          "Content-Type": "application/json",
        },
      })
      .then((res) => {
        resolve(res);

      }).catch(err => {
        reject(err)
      });
  });
}

export function formPost(url, params) {
  return new Promise((resolve, reject) => {
    axios
      .post(url, params, {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      })
      .then((res) => {
        resolve(res);

      }).catch(err => {
        reject(err)
      });
  });
}